using System;
using System.Collections.Generic;

namespace HtmlUtilities.UITest
{
    public static class HtmlObjectCommands
    {
        private static Action<string> CommandResult;

        public static IEnumerable<Command<string>> GetCommands(Action<string> resultAction)
        {
            CommandResult = resultAction;
            return new List<Command<string>>
                       {
                           new Command<string>("AttachEvent1", AttachEvent1),
                           new Command<string>("AttachEvent2", AttachEvent2),
                           new Command<string>("DetachEvent1", DetachEvent1),
                           new Command<string>("DetachEvent2", DetachEvent2)
                       };
        }

        private static IHtmlElement TestDiv { get { return Html.Page.Document.GetElementById("testing"); } }

        private static string DetachEvent2()
        {
            var link = Html.Page.Document.GetElementById("AttachEventLink2");
            if (link != null)
                link.DetachEvent("mouseover", MouseOverOnLink2);

            return "Detached MouseOver event on link";
        }

        private static string AttachEvent2()
        {
            var link = NewLink("AttachEventLink2", " MouseOver Me (2)");

            link.AttachEvent("mouseover", MouseOverOnLink2);

            return "Attached MouseOver event on link";
        }

        private static IHtmlElement NewLink(string name, string content)
        {
            var link = Html.Page.Document.GetElementById(name);
            if (link == null)
            {
                link = Html.Page.Document.CreateElement("a");
                link.Id = name;
                link.SetProperty("innerHTML", content);

                TestDiv.AppendChild(link);
            }

            return link;
        }

        private static string DetachEvent1()
        {
            var link = Html.Page.Document.GetElementById("AttachEventLink1");
            if (link != null)
                link.DetachEvent("mouseover", MouseOverOnLink);

            return "Detached MouseOver event on link";
        }

        private static string AttachEvent1()
        {
            var link = NewLink("AttachEventLink1", " MouseOver Me (1)");

            link.AttachEvent("mouseover", MouseOverOnLink);

            return "Attached MouseOver event on link";
        }

        private static void MouseOverOnLink(object sender, EventArgs e)
        {
            CommandResult(", MouseOver");
        }

        private static void MouseOverOnLink2(object sender, IHtmlEventArgs e)
        {
            CommandResult(", MouseOver (" + e.ScreenX + ", " + e.ScreenY + ")");
        }
    }
}